/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package awweaver.clanplugin;

import java.io.File;
import java.util.ArrayList;
import org.bukkit.entity.Player;
import org.bukkit.util.config.Configuration;
/**
 *
 * @author awweaver
 */
public class Clan {
    
    private final static int LEADER = 0;
    private final static int OFFICER_RANK = 1;
    private final static int MEMBER_RANK = 2;
    private static ClanPlugin plugin;
    protected Configuration config = new Configuration(new File(plugin.getDataFolder() + "config.yml"));
    private Player player;
    protected String clanName = new String();
    //Array members = new Array();
    //clan types
    protected boolean inviteOnly, warDeclared, openClan, closedClan, coreGroup;
    protected boolean mercenaries, police, criminal, military;
    //protected static final String mainDirectory = "plugins/ClanWars";
    //static protected File clanWars = new File(mainDirectory + File.separator + "config.yml");
    //protected Configuration config = new Configuration(clanWars);
    protected ArrayList policeList, mercenariesList, criminalList, militaryList;
    protected boolean neutralground = false;

    public Clan(String clanName1, Player player) 
    {
        
        config.setProperty(clanName, null);
        config.setProperty(clanName + ".Members." + player.getName(), LEADER); 
        
        config.save();
        inviteOnly = config.getBoolean("InviteOnly", false);
        warDeclared = false;
        openClan = true;
        closedClan = false;
        coreGroup = false;
        player.sendMessage("You are now apart of" +  getClanName(player));
    }
    public Clan(String clanName, Player player, String type)
    {
        config.setProperty(clanName, null);
        config.setProperty(clanName + ".Members.", player.getName() + LEADER);
        config.setProperty("Clan." + clanName + ".Type", type);
    }
    public void renameClan(String newName) 
    {
        clanName = newName;
        player.sendMessage(clanName + " is now the name of your clan");
    }
    public void setClanTag(String str, Player player)
    {
        config.setProperty("Clans." + getClanName(player) + ".", str);
        player.sendMessage("Clan Tag for " + getClanName(player) + " is set to " + str);
    }
    public String getClanTag(String str)
    {
        return config.getString("Clans." + getClanName(player) + ".Tag");
    }
    public boolean getType(Player player)
    {
        return config.getBoolean("Clans." + getClanName(player) + ".Type", true);
        
    }
    public void changeType(String type, Player player)
    {
        config.load();
        if ("invite".equals(type)) 
        {
            inviteOnly = true;
            openClan = false;
            closedClan = false;
            player.sendMessage(getClanName(player) + " is now Invite Only");
        }
        if ("open".equals(type)) 
        {
            inviteOnly = false;
            openClan = true;
            closedClan = false;
            player.sendMessage(getClanName(player) + " is now an Open Clan");
        }
        if ("closed".equals(type)) 
        {
            inviteOnly = false;
            openClan = false;
            closedClan = true;
            player.sendMessage(getClanName(player) + " is now a Closed Clan, no one will be able to join, and you can NOT send out invites");
        }
    }
    public void changetType(String type, Player player, String name)
    {
        if ("core".equals(type) && player.isOp()) 
        {
            coreGroup = true;
            player.sendMessage(name + " is now a Core Group");
        }
        if ("neutral".equals(type) && player.isOp()) 
        {
            neutralground = true;
            player.sendMessage("This is now Neutral Ground");
            //Disable pvp
        }
    }
    public void leaveClan(Player player)
    {
        player.sendMessage("You have now left" + getClanName(player));
        config.removeProperty("Clans." + getClanName(player) + ".Memebers." + player.getName());  // may or may not work
        
    }

    public void applyToClan(String clanName, Player player) 
    {
        if (closedClan) 
        {
            player.sendMessage("This is a closed clan.");
            return;
        }
        if (openClan) 
        {
            config.setProperty("Clans." + clanName + ".", player.getName());
            player.sendMessage("You are now apart of " + clanName);
            return;
        }
        if (inviteOnly)
        {
            //later
        }

        config.save();
    }

    public void inviteMember(String name) 
    {
        
        config.save();
    }
    public void listMembers()
    {
        ArrayList list = new ArrayList();
        config.getStringList("Clans." + getClanName(player) + ".Members.", list);
        player.sendMessage(list.toString());
    }
    public void listMembers(String clanName)
    {
        ArrayList list = new ArrayList();
        config.getStringList("Clans." + clanName + ".Members.", list);
        player.sendMessage(list.toString());
    }

    public void addMember(String playerName, Player player, int rank)
    {
        config.setProperty("Clans" + getClanName(player) + ".Members." + playerName , rank);
        if(rank == OFFICER_RANK)
        {
            player.sendMessage(playerName + " has been added to " + clanName + " at rank Officer");
        }
        else if(rank == MEMBER_RANK)
        {
            player.sendMessage(playerName + " has been added to " + clanName + " at rank Member");
            
        }
        
        config.save();
    }
    public void removeMember(String playerName) 
    {
        config.removeProperty("Clans." + getClanName(player) + ".Memebers." + playerName);
        player.sendMessage(playerName + " has been removed from " + clanName);
    }

    public void disbandClan(Player player) 
    {
        config.removeProperty(getClanName(player));
        // ONLY LEADER ALLOWED TO DO THIS
    }

    public void declareWar(String side2) 
    {
        if (!isWarDeclared(getClanName(player))) 
        {
            warDeclared = true;
            player.sendMessage("you have declared war on " + side2);
        }
    }
    public void createArena()
    {
        // used the wand from world edit if possible
        // Options: PvP is ONLY on when war is declared, and ONLY for the members of those clans
    }
    public String getRank()
    {
        int rank = (config.getInt("Clans." + getClanName(player) + ".Members." + player.getName());
        if(
        return;
    }
    private String getClanName(Player player) 
    {
        return (String) config.getProperty("Clans.");

    public boolean isWarDeclared(String clanName) 
    {
        return config.getBoolean("WarDeclared", false);
    }

    public void listMembers(String clanName) 
    {
       //if("".equals(clanName))
      //  {..
            // once file input works propperly
     //   }
    }

    public void listClans()
    {
        ArrayList clans = new ArrayList();
        config.getStringList("Clans.", clans);
        
        player.sendMessage(clans.toString());
        // file input
    }

    public boolean isLeader(Player player) 
    {
        boolean b = false;
        if(config.getProperty("Leader") == player.getName())
        {
            b = true;
        }
        return b;
    }
    public void promoteMember(String playerName, String rank)
    {
        // use numbers for ranks?  Leader = 0 Officer = 1 Member = 2
    }
    public void demoteMember(String playerName, String rank)
    {
        
    }
}
